package com.grj.template.boot.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.grj.template.boot.domain.Permission;
import com.grj.template.boot.service.PermissionService;
import com.grj.template.boot.mapper.PermissionMapper;
import com.grj.template.boot.util.StringUtil;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author eren
 * @description 针对表【permission】的数据库操作Service实现
 * @createDate 2024-10-21 15:45:24
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission>
        implements PermissionService {

    @Override
    public Page<Permission> fetchPermissionPage(int currentPage, int pageSize, Permission permission, List<Date> during) {
        QueryWrapper<Permission> permissionQueryWrapper = Wrappers.query();
        if (during != null && !during.isEmpty()) {
            permissionQueryWrapper.between("create_time", during.get(0), during.get(1));
        }
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(permission);
        HashMap<String, Object> map = new HashMap<>();
        stringObjectMap.forEach((k, v) -> {
            if (v != null) {
                map.put(StringUtil.toCamelCase(k), v);
            }
        });
        permissionQueryWrapper.allEq(map, false);
        return baseMapper.selectPage(new Page<>(currentPage, pageSize), permissionQueryWrapper);
    }

    @Override
    public void postPermission(Permission permission) {
        baseMapper.insert(permission);
    }

    @Override
    public void putPermission(Permission permission) {
        baseMapper.updateById(permission);
    }

    @Override
    public void delPermission(Long id) {
        List<Permission> permissions = baseMapper.selectList(Wrappers.query(Permission.class).eq("parent_id", id));
        if (!permissions.isEmpty()) {
            throw new RuntimeException("该权限下存在子权限，无法删除");
        }
        baseMapper.deleteById(id);
    }

    @Override
    public Permission fetchPermissionById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<Permission> fetchPermissionTree() {
        List<Permission> permissionList = baseMapper.selectList(null);
        return permissionList.stream().filter(p1 -> p1.getPermissionType().equals("0")).map(contents -> {
            List<Permission> menuList = permissionList.stream().filter(p2 -> p2.getPermissionType().equals("1") && p2.getParentId().equals(contents.getPermissionId())).map(menu -> {
                List<Permission> buttonList = permissionList.stream().filter(p3-> p3.getPermissionType().equals("2") && p3.getParentId().equals(contents.getPermissionId())).collect(Collectors.toList());
                menu.setChildren(buttonList);
                return menu;
            }).collect(Collectors.toList());
            contents.setChildren(menuList);
            return contents;
        }).collect(Collectors.toList());
    }

    @Override
    public List<Permission> fetchPermissionMenu() {
        List<Permission> permissionList = baseMapper.selectList(Wrappers.query(Permission.class).eq("permission_type", "0").or().eq("permission_type", "1"));
        return permissionList.stream().filter(p1 -> p1.getPermissionType().equals("0")).map(contents -> {
            List<Permission> menuList = permissionList.stream().filter(p2 -> p2.getPermissionType().equals("1") && p2.getParentId().equals(contents.getPermissionId())).collect(Collectors.toList());
            contents.setChildren(menuList);
            return contents;
        }).collect(Collectors.toList());
    }

    @Override
    public List<Permission> fetchPermissionMenuByIds(List<Long> permissionIds) {
        List<Permission> permissionList = baseMapper.selectList(Wrappers.lambdaQuery(Permission.class).in(Permission::getPermissionId, permissionIds).and(lambdaQueryWrapper->{
            lambdaQueryWrapper.eq(Permission::getPermissionType, "0").or().eq(Permission::getPermissionType, "1");
        }));
        return permissionList.stream().filter(p1 -> p1.getPermissionType().equals("0")).map(contents -> {
            List<Permission> menuList = permissionList.stream().filter(p2 -> p2.getPermissionType().equals("1") && p2.getParentId().equals(contents.getPermissionId())).collect(Collectors.toList());
            contents.setChildren(menuList);
            return contents;
        }).collect(Collectors.toList());
    }
}




